{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 人脸识别与计算机视觉文档实践\n",
    "\n",
    ">* 本周主要内容：人脸（Face） API文档不同平台对比与实践及计算机视觉入门（认知服务）\n",
    ">* 202009_API_人工智能与机器学习_week03\n",
    ">*  电子讲义设计者：许智超\n",
    "<br/>\n",
    "<br/>\n",
    "\n",
    "## 复习\n",
    "\n",
    "* 上周主要内容： \n",
    ">    * 1、API文档\n",
    ">    * 2、认知服务-人脸识别\n",
    ">    * 3、pandas黑魔法（json_normalize）\n",
    "-----\n",
    "* 提问、检查与扩展：\n",
    ">    * 1、上周的Azure face API随机检查2位同学，对于API文档是否有能力详细阅读？\n",
    ">    * 2、抽查2位同学，对于获取的数据用途有何思考？是否尝试其他平台（XXX API、XXX API）      \n",
    ">    * 3、扩展: 我们尝试抽取了4张图片，包含AB、ABC、BCD、ABCD四个人物特征，如何通过API检查数据差异？\n",
    "  \n",
    "\n",
    "-----\n",
    "## 本周学习目标：\n",
    "\n",
    "> $\\mathcal{1、Azure 认知服务-人脸集合演示}$     \n",
    "> $\\mathcal{2、Face++ 之 FaceSets 实践}$     \n",
    "> $\\mathcal{3、计算机视觉实践}$    \n",
    "\n",
    "\n",
    "\n",
    "## 学生权限\n",
    "\n",
    "* [访问学生权益](https://azure.microsoft.com/zh-cn/education/)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       "/* 本电子讲义使用之CSS */\n",
       "div.code_cell {\n",
       "    background-color: #e5f1fe;\n",
       "}\n",
       "div.cell.selected {\n",
       "    background-color: #effee2;\n",
       "    font-size: 2rem;\n",
       "    line-height: 2.4rem;\n",
       "}\n",
       "div.cell.selected .rendered_html table {\n",
       "    font-size: 2rem !important;\n",
       "    line-height: 2.4rem !important;\n",
       "}\n",
       ".rendered_html pre code {\n",
       "    background-color: #C4E4ff;   \n",
       "    padding: 2px 25px;\n",
       "}\n",
       ".rendered_html pre {\n",
       "    background-color: #99c9ff;\n",
       "}\n",
       "div.code_cell .CodeMirror {\n",
       "    font-size: 2rem !important;\n",
       "    line-height: 2.4rem !important;\n",
       "}\n",
       ".rendered_html img, .rendered_html svg {\n",
       "    max-width: 50%;\n",
       "    height: auto;\n",
       "    float: center;\n",
       "}\n",
       "/* Gradient transparent - color - transparent */\n",
       "hr {\n",
       "    border: 0;\n",
       "    border-bottom: 1px dashed #ccc;\n",
       "}\n",
       ".emoticon{\n",
       "    font-size: 5rem;\n",
       "    line-height: 4.4rem;\n",
       "    text-align: center;\n",
       "    vertical-align: middle;\n",
       "}\n",
       "\n",
       "</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<style>\n",
    "/* 本电子讲义使用之CSS */\n",
    "div.code_cell {\n",
    "    background-color: #e5f1fe;\n",
    "}\n",
    "div.cell.selected {\n",
    "    background-color: #effee2;\n",
    "    font-size: 2rem;\n",
    "    line-height: 2.4rem;\n",
    "}\n",
    "div.cell.selected .rendered_html table {\n",
    "    font-size: 2rem !important;\n",
    "    line-height: 2.4rem !important;\n",
    "}\n",
    ".rendered_html pre code {\n",
    "    background-color: #C4E4ff;   \n",
    "    padding: 2px 25px;\n",
    "}\n",
    ".rendered_html pre {\n",
    "    background-color: #99c9ff;\n",
    "}\n",
    "div.code_cell .CodeMirror {\n",
    "    font-size: 2rem !important;\n",
    "    line-height: 2.4rem !important;\n",
    "}\n",
    ".rendered_html img, .rendered_html svg {\n",
    "    max-width: 50%;\n",
    "    height: auto;\n",
    "    float: center;\n",
    "}\n",
    "/* Gradient transparent - color - transparent */\n",
    "hr {\n",
    "    border: 0;\n",
    "    border-bottom: 1px dashed #ccc;\n",
    "}\n",
    ".emoticon{\n",
    "    font-size: 5rem;\n",
    "    line-height: 4.4rem;\n",
    "    text-align: center;\n",
    "    vertical-align: middle;\n",
    "}\n",
    "\n",
    "</style>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 复习\n",
    "\n",
    "> 1、回顾阅读API文档的关键点     \n",
    "> 2、正确阅读json数据 [jsonviewer.stack.hu(json转换检查数据)](http://jsonviewer.stack.hu)    \n",
    "> 3、pandas 中的json_normalize模块/函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Azure 认知服务-人脸演示"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 试一试：人脸验证(难)\n",
    "\n",
    "\n",
    "\n",
    ">* API人脸文档中最重要的组成部分：\n",
    " >>* Request URL？\n",
    " >>* Http Method？\n",
    " >>* 参数？\n",
    "\n",
    "----\n",
    ">* 不同的人脸对比数据\n",
    "  >>* 人脸验证关键数据？\n",
    "  >>* 根据哪些数据证明是同一个人？\n",
    "    \n",
    "----\n",
    ">* 具体步骤：\n",
    "  >>* 1、Create 请求成功200 返回空字符串\n",
    "  >>* 2、Add face 请求成功200 返回persistedFaceId\n",
    "  >>* 3、Detect 准备 被检测人 人脸的id\n",
    "  >>* 4、Find similars 返回相似置信度\n",
    "  >>* 附加：get查看facelists\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "- [API中文文档](https://docs.microsoft.com/zh-cn/rest/api/cognitiveservices/face/facelist/create#code-try-0)\n",
    "- [API英文文档](https://southeastasia.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039524b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、Create 请求成功200 返回空字符串\n",
    "> 在开始检测之前，我们需要构建一个列表用于装取我们需要进行对比的人脸\n",
    "- [创建人脸列表api文档](https://docs.microsoft.com/zh-cn/rest/api/cognitiveservices/face/facelist/create)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "# 1、create  列表\n",
    "# faceListId\n",
    "\n",
    "create_facelists_url = \"https://api-hly.cognitiveservices.azure.com/face/v1.0/facelists/faces_1021b\"\n",
    "subscription_key = \"89066c5729ec4449997c1e89c0fbefb8\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#create 创建一个装相片的列表\n",
    "data = {\n",
    "    \"name\": \"list_faces_1021b\",\n",
    "    \"userData\": \"同学们\",\n",
    "    \"recognitionModel\": \"recognition_02\"\n",
    "}\n",
    "params = {\n",
    "    \"faceListId\":\"faces_1021b\"\n",
    "}\n",
    "r_create = requests.put(create_facelists_url,headers=headers,params=params,json=data) \n",
    "r_create.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 已经成功创建facelist（云端/云计算）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2、Add face 请求成功200 返回persistedFaceId\n",
    "> 我们通过上面的步骤建好了一个脸的列表，接下来我们要给这个列表添加脸了！把我们想要对比的脸存进列表吧\n",
    "- [添加人脸进列表api文档](https://docs.microsoft.com/zh-cn/rest/api/cognitiveservices/face/facelist/addfacefromurl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先加一张脸试试\n",
    "# 2、Add face\n",
    "\n",
    "add_face_url = \"https://api-hly.cognitiveservices.azure.com/face/v1.0/facelists/faces_1021a/persistedfaces\" \n",
    "subscription_key = \"89066c5729ec4449997c1e89c0fbefb8\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "img_url = \"http://hong_ling_ya.gitee.io/api-1021/picture/xiaonvhai.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"faces_1021b\",\n",
    "    \"userData\":\"小女孩\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 扩展内容，封装成函数方便多次使用 *\n",
    "> 我们要添加多张脸，但是为了减少代码量，我们可以把代码封装成函数，避免每次都要写一大堆代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://api-hly.cognitiveservices.azure.com/face/v1.0/facelists/faces_1021a/persistedfaces\"\n",
    "    subscription_key = \"89066c5729ec4449997c1e89c0fbefb8\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"faceListId\":\"faces_1021b\",\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"http://hong_ling_ya.gitee.io/api-1021/picture/troye.jpg\",\"戳爷troye sivan\")\n",
    "AddFace(\"http://hong_ling_ya.gitee.io/api-1021/picture/taylor.jpg\",\"霉霉taylor swift\")\n",
    "AddFace(\"http://hong_ling_ya.gitee.io/api-1021/picture/heiren.jpg\",\"黑人小孩\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 附加：get查看facelists\n",
    "> 查看列表\n",
    "- [查看人脸列表api文档](https://docs.microsoft.com/zh-cn/rest/api/cognitiveservices/face/facelist/get)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get facelist\n",
    "def GetFacelist():\n",
    "    get_facelist_url = \"https://api-hly.cognitiveservices.azure.com/face/v1.0/facelists/faces_1021b\"\n",
    "    date = {\n",
    "        \"faceListId\":\"faces_1021b\",\n",
    "    }\n",
    "    r_get_facelist = requests.get(get_facelist_url,headers=headers,params=data)\n",
    "    return r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [],\n",
       " 'faceListId': 'faces_1021b',\n",
       " 'name': 'list_faces_1021b',\n",
       " 'userData': '同学们'}"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GetFacelist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"http://hong_ling_ya.gitee.io/api-1021/picture/me.jpg\",\"我自己\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4、Find similars 返回相似置信度\n",
    "- [监测人脸相似度api文档](https://docs.microsoft.com/zh-cn/rest/api/cognitiveservices/face/face/findsimilar)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[{\"faceId\": \"7333315c-575a-4c2c-a5c0-6b5428a80af8\", \"faceRectangle\": {\"top\": 66, \"left\": 97, \"width\": 37, \"height\": 37}, \"faceAttributes\": {\"smile\": 0.115, \"headPose\": {\"pitch\": 7.2, \"roll\": 30.3, \"yaw\": 4.6}, \"gender\": \"female\", \"age\": 20.0, \"facialHair\": {\"moustache\": 0.0, \"beard\": 0.0, \"sideburns\": 0.0}, \"glasses\": \"ReadingGlasses\", \"emotion\": {\"anger\": 0.0, \"contempt\": 0.036, \"disgust\": 0.0, \"fear\": 0.0, \"happiness\": 0.115, \"neutral\": 0.845, \"sadness\": 0.003, \"surprise\": 0.001}, \"blur\": {\"blurLevel\": \"medium\", \"value\": 0.56}, \"exposure\": {\"exposureLevel\": \"goodExposure\", \"value\": 0.57}, \"noise\": {\"noiseLevel\": \"low\", \"value\": 0.0}, \"makeup\": {\"eyeMakeup\": false, \"lipMakeup\": true}, \"accessories\": [{\"type\": \"glasses\", \"confidence\": 0.99}], \"occlusion\": {\"foreheadOccluded\": false, \"eyeOccluded\": false, \"mouthOccluded\": false}, \"hair\": {\"bald\": 0.19, \"invisible\": false, \"hairColor\": [{\"color\": \"black\", \"confidence\": 0.98}, {\"color\": \"brown\", \"confidence\": 0.63}, {\"color\": \"other\", \"confidence\": 0.55}, {\"color\": \"gray\", \"confidence\": 0.37}, {\"color\": \"blond\", \"confidence\": 0.14}, {\"color\": \"red\", \"confidence\": 0.07}, {\"color\": \"white\", \"confidence\": 0.0}]}}}]'"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、检测人脸的id\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://api-hly.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文\n",
    "image_url = 'http://hong_ling_ya.gitee.io/api-1021/picture/me.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择model\n",
    "    'recognitionModel':'recognition_02',#此参数需与facelist参数一致\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->字符串\n",
    "json.dumps(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "我自己 = response.json()[0][\"faceId\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [],
   "source": [
    "#4.Find similars\n",
    "\n",
    "findsimilars_url = \"https://api-hly.cognitiveservices.azure.com/face/v1.0/findsimilars\"\n",
    "\n",
    "# 请求正文 faceId需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":我自己,#取上方的faceID\n",
    "    \"faceListId\": \"faces_1021a\",\n",
    "    \"maxNumOfCandidatesReturned\": 10,\n",
    "    \"mode\": \"matchFace\"#matchPerson #一种为验证模式，一种为相似值模式\n",
    "    }\n",
    "\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': 'caa5538a-e0a1-49df-88ec-aea0be61c0b5',\n",
       "  'confidence': 1.0},\n",
       " {'persistedFaceId': '17fe48f8-a91a-4660-b03a-d8a7d128b562',\n",
       "  'confidence': 1.0},\n",
       " {'persistedFaceId': '3317643c-e86e-4c8f-92e2-20e8f5cbd8be',\n",
       "  'confidence': 0.09057},\n",
       " {'persistedFaceId': '5692a418-fbaa-4f15-9438-3ee90a72c8b1',\n",
       "  'confidence': 0.09057},\n",
       " {'persistedFaceId': '25408710-8a01-4ebd-8430-847b9c043326',\n",
       "  'confidence': 0.08571},\n",
       " {'persistedFaceId': '9fbef435-53ea-4d43-a0a7-e01639e79830',\n",
       "  'confidence': 0.08571},\n",
       " {'persistedFaceId': 'c2ecc026-ba6f-47a3-94a1-5bfb0fab5f39',\n",
       "  'confidence': 0.08571},\n",
       " {'persistedFaceId': '040167e7-36e6-4eef-8936-d3ace27fa816',\n",
       "  'confidence': 0.08571},\n",
       " {'persistedFaceId': 'b8aa72e4-43d1-4df0-a405-b2d4a8bab73d',\n",
       "  'confidence': 0.08297},\n",
       " {'persistedFaceId': '6fe39617-aa47-4592-8be1-f0f7818cbd33',\n",
       "  'confidence': 0.08297}]"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [],
   "source": [
    "#6.1将人脸数据库做成表格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import pandas as pd\n",
    "import json\n",
    "# 1、create  列表\n",
    "# faceListId\n",
    "\n",
    "create_facelists_url = \"https://api-hly.cognitiveservices.azure.com/face/v1.0/facelists/faces_1021a\"\n",
    "subscription_key = \"89066c5729ec4449997c1e89c0fbefb8\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '25408710-8a01-4ebd-8430-847b9c043326',\n",
       "   'userData': '小女孩'},\n",
       "  {'persistedFaceId': '9fbef435-53ea-4d43-a0a7-e01639e79830',\n",
       "   'userData': '小女孩'},\n",
       "  {'persistedFaceId': 'c2ecc026-ba6f-47a3-94a1-5bfb0fab5f39',\n",
       "   'userData': '小女孩'},\n",
       "  {'persistedFaceId': 'b8aa72e4-43d1-4df0-a405-b2d4a8bab73d',\n",
       "   'userData': '霉霉taylor swift'},\n",
       "  {'persistedFaceId': '3317643c-e86e-4c8f-92e2-20e8f5cbd8be',\n",
       "   'userData': '黑人小孩'},\n",
       "  {'persistedFaceId': 'caa5538a-e0a1-49df-88ec-aea0be61c0b5',\n",
       "   'userData': '我自己'},\n",
       "  {'persistedFaceId': '040167e7-36e6-4eef-8936-d3ace27fa816',\n",
       "   'userData': '小女孩'},\n",
       "  {'persistedFaceId': '6fe39617-aa47-4592-8be1-f0f7818cbd33',\n",
       "   'userData': '霉霉taylor swift'},\n",
       "  {'persistedFaceId': '5692a418-fbaa-4f15-9438-3ee90a72c8b1',\n",
       "   'userData': '黑人小孩'},\n",
       "  {'persistedFaceId': '17fe48f8-a91a-4660-b03a-d8a7d128b562',\n",
       "   'userData': '我自己'}],\n",
       " 'faceListId': 'faces_1021a',\n",
       " 'name': 'list_faces_1021a',\n",
       " 'userData': '同学们'}"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_facelist_url = \"https://api-hly.cognitiveservices.azure.com/face/v1.0/facelists/faces_1021a\"\n",
    "\n",
    "data = {\n",
    "    \"faceListId\":\"faces_1021a\",\n",
    "}\n",
    "r_get_facelist = requests.get(get_facelist_url,headers=headers,params=data)\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import pandas as pd\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.1.3'"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>25408710-8a01-4ebd-8430-847b9c043326</td>\n",
       "      <td>小女孩</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9fbef435-53ea-4d43-a0a7-e01639e79830</td>\n",
       "      <td>小女孩</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>c2ecc026-ba6f-47a3-94a1-5bfb0fab5f39</td>\n",
       "      <td>小女孩</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b8aa72e4-43d1-4df0-a405-b2d4a8bab73d</td>\n",
       "      <td>霉霉taylor swift</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3317643c-e86e-4c8f-92e2-20e8f5cbd8be</td>\n",
       "      <td>黑人小孩</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>caa5538a-e0a1-49df-88ec-aea0be61c0b5</td>\n",
       "      <td>我自己</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>040167e7-36e6-4eef-8936-d3ace27fa816</td>\n",
       "      <td>小女孩</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6fe39617-aa47-4592-8be1-f0f7818cbd33</td>\n",
       "      <td>霉霉taylor swift</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>5692a418-fbaa-4f15-9438-3ee90a72c8b1</td>\n",
       "      <td>黑人小孩</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>17fe48f8-a91a-4660-b03a-d8a7d128b562</td>\n",
       "      <td>我自己</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId        userData\n",
       "0  25408710-8a01-4ebd-8430-847b9c043326             小女孩\n",
       "1  9fbef435-53ea-4d43-a0a7-e01639e79830             小女孩\n",
       "2  c2ecc026-ba6f-47a3-94a1-5bfb0fab5f39             小女孩\n",
       "3  b8aa72e4-43d1-4df0-a405-b2d4a8bab73d  霉霉taylor swift\n",
       "4  3317643c-e86e-4c8f-92e2-20e8f5cbd8be            黑人小孩\n",
       "5  caa5538a-e0a1-49df-88ec-aea0be61c0b5             我自己\n",
       "6  040167e7-36e6-4eef-8936-d3ace27fa816             小女孩\n",
       "7  6fe39617-aa47-4592-8be1-f0f7818cbd33  霉霉taylor swift\n",
       "8  5692a418-fbaa-4f15-9438-3ee90a72c8b1            黑人小孩\n",
       "9  17fe48f8-a91a-4660-b03a-d8a7d128b562             我自己"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#facelist里面的数据\n",
    "adf = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])# 升级pandas才能运行\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>caa5538a-e0a1-49df-88ec-aea0be61c0b5</td>\n",
       "      <td>1.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>17fe48f8-a91a-4660-b03a-d8a7d128b562</td>\n",
       "      <td>1.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3317643c-e86e-4c8f-92e2-20e8f5cbd8be</td>\n",
       "      <td>0.09057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5692a418-fbaa-4f15-9438-3ee90a72c8b1</td>\n",
       "      <td>0.09057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>25408710-8a01-4ebd-8430-847b9c043326</td>\n",
       "      <td>0.08571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>9fbef435-53ea-4d43-a0a7-e01639e79830</td>\n",
       "      <td>0.08571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>c2ecc026-ba6f-47a3-94a1-5bfb0fab5f39</td>\n",
       "      <td>0.08571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>040167e7-36e6-4eef-8936-d3ace27fa816</td>\n",
       "      <td>0.08571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>b8aa72e4-43d1-4df0-a405-b2d4a8bab73d</td>\n",
       "      <td>0.08297</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>6fe39617-aa47-4592-8be1-f0f7818cbd33</td>\n",
       "      <td>0.08297</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  confidence\n",
       "0  caa5538a-e0a1-49df-88ec-aea0be61c0b5     1.00000\n",
       "1  17fe48f8-a91a-4660-b03a-d8a7d128b562     1.00000\n",
       "2  3317643c-e86e-4c8f-92e2-20e8f5cbd8be     0.09057\n",
       "3  5692a418-fbaa-4f15-9438-3ee90a72c8b1     0.09057\n",
       "4  25408710-8a01-4ebd-8430-847b9c043326     0.08571\n",
       "5  9fbef435-53ea-4d43-a0a7-e01639e79830     0.08571\n",
       "6  c2ecc026-ba6f-47a3-94a1-5bfb0fab5f39     0.08571\n",
       "7  040167e7-36e6-4eef-8936-d3ace27fa816     0.08571\n",
       "8  b8aa72e4-43d1-4df0-a405-b2d4a8bab73d     0.08297\n",
       "9  6fe39617-aa47-4592-8be1-f0f7818cbd33     0.08297"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回相似度的数据\n",
    "bdf = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行\n",
    "bdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>caa5538a-e0a1-49df-88ec-aea0be61c0b5</td>\n",
       "      <td>我自己</td>\n",
       "      <td>1.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>17fe48f8-a91a-4660-b03a-d8a7d128b562</td>\n",
       "      <td>我自己</td>\n",
       "      <td>1.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3317643c-e86e-4c8f-92e2-20e8f5cbd8be</td>\n",
       "      <td>黑人小孩</td>\n",
       "      <td>0.09057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>5692a418-fbaa-4f15-9438-3ee90a72c8b1</td>\n",
       "      <td>黑人小孩</td>\n",
       "      <td>0.09057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>25408710-8a01-4ebd-8430-847b9c043326</td>\n",
       "      <td>小女孩</td>\n",
       "      <td>0.08571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9fbef435-53ea-4d43-a0a7-e01639e79830</td>\n",
       "      <td>小女孩</td>\n",
       "      <td>0.08571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>c2ecc026-ba6f-47a3-94a1-5bfb0fab5f39</td>\n",
       "      <td>小女孩</td>\n",
       "      <td>0.08571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>040167e7-36e6-4eef-8936-d3ace27fa816</td>\n",
       "      <td>小女孩</td>\n",
       "      <td>0.08571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b8aa72e4-43d1-4df0-a405-b2d4a8bab73d</td>\n",
       "      <td>霉霉taylor swift</td>\n",
       "      <td>0.08297</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6fe39617-aa47-4592-8be1-f0f7818cbd33</td>\n",
       "      <td>霉霉taylor swift</td>\n",
       "      <td>0.08297</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId        userData  confidence\n",
       "5  caa5538a-e0a1-49df-88ec-aea0be61c0b5             我自己     1.00000\n",
       "9  17fe48f8-a91a-4660-b03a-d8a7d128b562             我自己     1.00000\n",
       "4  3317643c-e86e-4c8f-92e2-20e8f5cbd8be            黑人小孩     0.09057\n",
       "8  5692a418-fbaa-4f15-9438-3ee90a72c8b1            黑人小孩     0.09057\n",
       "0  25408710-8a01-4ebd-8430-847b9c043326             小女孩     0.08571\n",
       "1  9fbef435-53ea-4d43-a0a7-e01639e79830             小女孩     0.08571\n",
       "2  c2ecc026-ba6f-47a3-94a1-5bfb0fab5f39             小女孩     0.08571\n",
       "6  040167e7-36e6-4eef-8936-d3ace27fa816             小女孩     0.08571\n",
       "3  b8aa72e4-43d1-4df0-a405-b2d4a8bab73d  霉霉taylor swift     0.08297\n",
       "7  6fe39617-aa47-4592-8be1-f0f7818cbd33  霉霉taylor swift     0.08297"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#合并在一起，得出班级能谁最像你\n",
    "pd.merge(adf, bdf,how='inner', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6rc1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "302px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
